package com.lidroid.xutils.bitmap.core;



import ohos.global.resource.Resource;
import ohos.media.image.PixelMap;

import ohos.media.image.ImageSource;
//import ohos.media.image.ImageSource.DecodingOptions;

import java.io.File;
import java.io.FileDescriptor;

public class BitmapDecoder {

    private BitmapDecoder() {
    }
    //options.inPurgeable = true;未找到
    public static PixelMap decodeSampledBitmapFromResource(Resource res, int resId, int reqWidth, int reqHeight) {
    /*    final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPurgeable = true;
        BitmapFactory.decodeResource(res, resId, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        try {
            return BitmapFactory.decodeResource(res, resId, options);
        } catch (OutOfMemoryError e) {
            LogUtils.e(e.getMessage());
            return null;
        }*/

        final ImageSource.DecodingOptions options = new ImageSource.DecodingOptions();
        ImageSource imageSource = ImageSource.create(res,null);
        int width=imageSource.getImageInfo().size.width;
        int height=imageSource.getImageInfo().size.height;
        options.sampleSize=calculateInSampleSize(width,height, reqWidth, reqHeight);
    return    imageSource.createPixelmap(options);

    }

    public static PixelMap decodeSampledBitmapFromFile(String filename, int reqWidth, int reqHeight) {
  /*      final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPurgeable = true;
        BitmapFactory.decodeFile(filename, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        try {
            return BitmapFactory.decodeFile(filename, options);
        } catch (OutOfMemoryError e) {
            LogUtils.e(e.getMessage());
            return null;
        }*/
        ImageSource imageSource=ImageSource.create(new File(filename),null);
        final ImageSource.DecodingOptions options = new ImageSource.DecodingOptions();
        int width=imageSource.getImageInfo().size.width;
        int height=imageSource.getImageInfo().size.height;
        options.sampleSize=calculateInSampleSize(width,height, reqWidth, reqHeight);
    return  imageSource.createPixelmap(options);
    }

    public static PixelMap decodeSampledBitmapFromDescriptor(FileDescriptor fileDescriptor, int reqWidth, int reqHeight) {
/*
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPurgeable = true;
        BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        try {
            return BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
        } catch (OutOfMemoryError e) {
            LogUtils.e(e.getMessage());
            return null;
        }*/
        ImageSource imageSource=ImageSource.create(fileDescriptor,null);
        final ImageSource.DecodingOptions options = new ImageSource.DecodingOptions();
        int width=imageSource.getImageInfo().size.width;
        int height=imageSource.getImageInfo().size.height;
        options.sampleSize=calculateInSampleSize(width,height, reqWidth, reqHeight);
        return  imageSource.createPixelmap(options);
    }

    public static int calculateInSampleSize(int width, int height,int reqWidth, int reqHeight) {

        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }

            final float totalPixels = width * height;

            final float totalReqPixelsCap = reqWidth * reqHeight * 2;

            while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
                inSampleSize++;
            }
        }
        return inSampleSize;
    }
}
